/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.cms.column.web;

import com.jeeplus.cms.support.TreeBuilder;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.cms.column.entity.CmsColumn;
import com.jeeplus.modules.cms.column.service.CmsColumnService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 栏目Controller
 *
 * @author Sawyer
 * @version 2020-11-21
 */
@RestController
@RequestMapping(value = "/cms/column/cmsColumn")
public class CmsColumnController extends BaseController {

    @Autowired
    private CmsColumnService cmsColumnService;

    @ModelAttribute
    public CmsColumn get(@RequestParam(required = false) String id) {
        CmsColumn entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = cmsColumnService.get(id);
        }
        if (entity == null) {
            entity = new CmsColumn();
        }
        return entity;
    }


    /**
     * 栏目树表数据
     */
    @RequiresPermissions("cms:column:cmsColumn:list")
    @GetMapping("list")
    public AjaxJson list(CmsColumn cmsColumn) {
        return AjaxJson.success().put("list", cmsColumnService.findList(cmsColumn));
    }

    /**
     * 根据Id获取栏目数据
     */
    @RequiresPermissions(value = {"cms:column:cmsColumn:view", "cms:column:cmsColumn:add", "cms:column:cmsColumn:edit"}, logical = Logical.OR)
    @GetMapping("queryById")
    public AjaxJson queryById(CmsColumn cmsColumn) {
        return AjaxJson.success().put("cmsColumn", cmsColumn);
    }

    /**
     * 保存栏目
     */
    @RequiresPermissions(value = {"cms:column:cmsColumn:add", "cms:column:cmsColumn:edit"}, logical = Logical.OR)
    @PostMapping("save")
    public AjaxJson save(CmsColumn cmsColumn, Model model) throws Exception {
        if (jeePlusProperites.isDemoMode()) {
            return AjaxJson.error("演示模式，不允许操作！");
        }
        String errMsg = beanValidator(cmsColumn);
        if (StringUtils.isNotBlank(errMsg)) {
            return AjaxJson.error(errMsg);
        }

        //新增或编辑表单保存
        cmsColumnService.save(cmsColumn);
        return AjaxJson.success("保存栏目成功");
    }

    /**
     * 删除栏目
     */
    @RequiresPermissions("cms:column:cmsColumn:del")
    @DeleteMapping("delete")
    public AjaxJson delete(CmsColumn cmsColumn) {
        if (jeePlusProperites.isDemoMode()) {
            return AjaxJson.error("演示模式，不允许操作！");
        }
        try {
            cmsColumnService.delete(cmsColumn);
            return AjaxJson.success("删除栏目成功");
        } catch (Exception e) {
            return AjaxJson.error(e.getMessage());
        }
    }

    /**
     * 获取JSON树形数据。
     */
    @RequiresPermissions("user")
    @GetMapping("treeData")
    public AjaxJson treeData(CmsColumn cmsColumn, @RequestParam(required = false) String excludeIds, @RequestParam(required = false) String includeIds) {
        return AjaxJson.success().put("treeData", TreeBuilder.buildTree(cmsColumnService.findList(cmsColumn), excludeIds, includeIds));
    }
}
